var selectedRow = '';
var isInit = true;

$(function () {
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            openInfoMenuPage();
        },
        'click .mod': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            openEditMenuPage();
        },
        'click .statusSet': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            changeState();
        },
        'click .delete' : function(e, value, row, index) {
            //删除操作
            selectedRow = row;
            deleteMenu();
        }
    }
});

function doRefresh() {
    var menuName = $('#menuName').val();
    var menuLevel = $('#menuLevel').val();
    var status = $('#status').val();
    selectedRow = '';
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'pfMenu/pageFindMenuData?t=' + new Date().getTime(),
        query: {
            menuName : menuName,
            menuLevel : menuLevel,
            status : status
        },
    });
}

function getParams(params) {
    var menuName = $('#menuName').val();
    var menuLevel = $('#menuLevel').val();
    var status = $('#status').val();
    var pageSize = params.pageSize;
    var pageNo = params.pageNumber;
    if (isInit) {
        isInit = false;
        pageSize = searchInfo.pageSize ? searchInfo.pageSize : params.pageSize;
        pageNo = searchInfo.pageNo ? searchInfo.pageNo : params.pageNumber;
    }
    return {
        menuName : menuName,
        menuLevel : menuLevel,
        status : status,
        pageSize : pageSize,
        pageNo : pageNo
    };
}

function openAddMenuPage() {
    openEditPage('add', 0);
}

function openEditMenuPage() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    openEditPage('edit', selectedRow.id);
}

function openInfoMenuPage() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    openEditPage('info', selectedRow.id);
}

function openEditPage(command, id) {
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + 'pfMenu/editPfMenuPage?type=' + command + '&id=' + id + '&' + $('#searchForm').serialize() + '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

function changeState() {
    if (!selectedRow.status || !selectedRow.id) {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    var status = 'enabled';
    if (selectedRow.status == 'enabled') {
        status = 'disabled';
        var postData = {
            parentId : selectedRow.id,
        }
        $.ajax({
            type: 'post',
            url: basePath + 'pfMenu/checkHasChildMenu',
            dataType: 'json',
            contentType: "application/json",
            data: JSON.stringify(postData),
            success : function (data) {
                if(data && data.isSuccess){
                    if (data.displayMsg == 'EXIST') {
                        toastr.warning('当前父菜单下有子菜单，无法冻结，请冻结相应子菜单后重试。');
                    } else {
                        setMenuState(selectedRow.id, status);
                    }
                }else if(data&&!data.isSuccess){
                    toastr.error(data.errorDesc);
                }else{
                    toastr.error(common_system_error);
                }
            },
            error: function (request) {
                toastr.error(common_system_error);
            }
        });
    } else {
        setMenuState(selectedRow.id, status);
    }
}

function setMenuState(id, status) {
    var postData = {
        id : id,
        status : status,
    };
    $.ajax({
        type: 'post',
        url: basePath + 'pfMenu/changeState',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success('数据修改成功');
                doRefresh();
            }else if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error('数据修改失败');
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function deleteMenu() {
    if (!selectedRow.id) {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    var postData = {
        parentId : selectedRow.id,
    };
    $.ajax({
        type: 'post',
        url: basePath + 'pfMenu/checkHasChildMenu',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                if (data.displayMsg == 'EXIST') {
                    toastr.warning('当前父菜单下有子菜单，无法删除，请删除相应子菜单后重试。');
                } else {
                    var strHtml = formatLevel(selectedRow.menuLevel) + '【' + selectedRow.menuName + '】当前处于' + formatStatus(selectedRow.status) + '状态，确认删除？';
                    if (selectedRow.menuLevel == '1') {

                    }
                    swalToastr('', strHtml, 'warning', doMenuDel);
                }
            }else if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error(common_system_error);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function doMenuDel() {
    var postData = {
        id : selectedRow.id,
    };
    $.ajax({
        type: 'post',
        url: basePath + 'pfMenu/deleteMenu',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success('删除成功');
                doRefresh();
            }else if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error('删除失败');
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

//------------------------------------------------------------------------------列表数据初始化
function formatLevel(value, row, index) {
    if (value == 1) {
        return '一级菜单';
    }
    if (value == 2) {
        return '二级菜单';
    }
    if (value == 3) {
        return '三级菜单';
    }
    return value;
}

function actionFormatter(value, row, index) {
    return '<a class="mod" >编辑</a>' + getSeparator() +'<a class="statusSet" >'+(row.status=='enabled'?'冻结':'恢复')+'</a>' + getSeparator() + '<a class="delete">删除</a>';
}

function formatStatus(value, row, index){
    var title;
    if (value == 'enabled') {
        title = "有效";
    } else {
        title = "无效";
    }
    return title;
}

// dataTable
$('#dataTable').on("click-row.bs.table", function (e, row, $element) {
    // checkBox 的勾选
    $('#dataTable .success').removeClass('success');//去除之前选中的行的，选中样式
    $element.addClass('success');//添加当前选中的 success样式用于区别
    changeStatueBtn(row.status == 'enabled',"冻结","恢复");
    selectedRow = row;
});